CODEDIGEST
Home Articles CodeDigest Tutorials InstallShield FAQs
Skip Navigation LinksHome » Article » ASP.Net Article » Using CKEditor 3.x[aka FCKeditor] in ASP.Net  Submit Articles and Win Geeky Prizes!!   You are not logged in.
Search
 

Sponsors
InstallShield
 

Product Spotlight
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Using/Integrating CKEditor 3.x[aka FCKeditor] in ASP.Net
Free Trial: InstallShield 2010 for Windows Installers Is InstallShield right for you? InstallShield handles your most complex installation requirements in minutes. Try it now.

By Satheesh Babu
Posted On Feb 04,2010
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 2
Category: ASP.Net
Print this article.

Subscribe to our feed!

Using CKEditor 3.x[aka FCKeditor] in ASP.Net

A Open Source JavaScript Rich Text Editor(RTF)

 

CKEditor is formerly called as FCKEditor. It is an open source WYSIWYG editor which is called FCKEditor till 2.x version and got renamed to a new brand called CKEditor and continued its evolution from 3.0 version.

According to CKEditor website,

CKEditor is a text editor to be used inside web pages. It's a WYSIWYG editor, which means that the text being edited on it looks as similar as possible to the results users have when publishing it. It brings to the web common editing features found on desktop editing applications like Microsoft Word and OpenOffice.

 

InstallShield

Read my articles on FCKEditor 2.x in codedigest below,

Integrating FCKeditor in ASP.Net Websites – Part 1

Integrating FCKeditor in ASP.Net Websites – Part 2

The new CKEditor includes all the features of FCKEditor and improved a lot in the areas like accessibility, performance and etc. Refer here to know more.

 

From an ASP.Net perspective, the CKEditor 3.x does not include a serverside(.dll) library but just clientside executables. Hence, an ASP.Net multiline textbox control can become a rich text editor in the client side by using CKEditor. We can also make a P tag or DIV tag as a rich text editor using the new CKEditor.

 

Moving forward, we will see how the new CKEditor can be used in an ASP.Net website.

Steps

1.      Open Visual Studio 2008, Click File >Website and choose ASP.Net Website. Choose the language of your choice.

2.      Download the latest CKEditor files from here. The current version is 3.1.

3.      Unzip the zip file. Copy the ckeditor folder into your visual studio solution. Refer below,

4.      Drag an ASP.Net textbox control into your aspx page. Set its TextMode property to MultiLine.

5.      To use the CKEditor features with the MultiLine textbox, we need to include ckeditor.js file into our webpage. Refer below,

<script type="text/javascript" src="ckeditor/ckeditor.js"></script>

 

6.      Next, we need to call the ckeditor javascript API to replace the multiline text box to an editor.

  CKEDITOR.replace('TextBox1');

 

The above function can be called from window.onload event.

<script type="text/javascript">

     window.onload=function()

     {

     CKEDITOR.replace('TextBox1');

     }

 </script>

 

 OR

Call the above method after your multiline textbox tag.

<form id="form1" runat="server">

    <div>

        <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server"></asp:TextBox>

    <script type="text/javascript">       

        CKEDITOR.replace('TextBox1');

    </script>

 

The HTML elements (P or DIV or textarea) will be replaced by an editor instance when the above code executed. The content of the editor will be posted back to HTML element(textarea or P or DIV) when the page is submitted and it can be retrieved using element’s ID on the server side, TextBox1.Text in our case. Multiple ckeditors can be added by passing its ID to replace function.

7.      That’s it! Execute the page and you can see the Ckeditor in action.

Refer below,

The final code will look like,

<head runat="server">

    <title></title>

    <script type="text/javascript" src="ckeditor/ckeditor.js"></script>    

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server"></asp:TextBox>

    <script type="text/javascript">       

        //CKEDITOR.replace('TextBox1');

    </script>

    </div>

    <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />

    </form>

</body>

</html>

 

With this knowledge, we will move forward and do some basic customization on the CKEditor.

 


Useful Books For Developers
Microsoft AJAX Library Essentials: Client-side ASP.NET AJAX 1.0 Explained More books..

Recent Articles

Customizing CKEditor

By default, the CKEditor includes large number of editor options. See the image above. Most of the time, we will not require all the above options to do our styling. We can customize the CKEditor like we do in previous versions.

 

1.      Customizing the editor inline in the webpage through replace function

By default, the editor includes 2 toolbar sets, Full and Basic. To set Basic toolbar,

<script type="text/javascript">

        CKEDITOR.replace('TextBox1', {toolbar:'Basic'});      

 </script>

 

Or, you can set your own toolbar set inline like below,

  <script type="text/javascript">

        var config = {

            toolbar:

                      [

                         ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink'],

                         ['UIColor'], '/',

                         ['Styles', 'Format', 'Font', 'FontSize']

                      ]

        };

        CKEDITOR.replace('TextBox1', config);      

    </script>

 

2.      Customizing the editor through config.js.

Browse to the config.js file in ckeditor folder. You can set some default setting here. Refer below,

CKEDITOR.editorConfig = function( config )

{

       // Define changes to default configuration here. For example:

       // config.language = 'fr';

       // config.uiColor = '#AADC6E';

};

 

To include your own toolbarset, add your own customized toolbar definition here. Refer below,

CKEDITOR.editorConfig = function( config )

{

       // Define changes to default configuration here. For example:

       // config.language = 'fr';

    // config.uiColor = '#AADC6E';

 

 

    config.toolbar_CodeDigestTool =

    [  

    ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],

    ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'],

    ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],

    ['Link', 'Unlink', 'Anchor'],

    ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],

    '/',

    ['Styles', 'Format', 'Font', 'FontSize'],

    ['TextColor', 'BGColor'],

    ['Maximize', 'ShowBlocks', '-', 'About']

    ];

 

};

 

In the above code, your new toolbar name will be “CodeDigestTool”(The word after “_”)

<script type="text/javascript">

        CKEDITOR.replace('TextBox1', {toolbar:'CodeDigestTool'});      

 </script>

 

Refer here to know all the config options.

 

Editing config.js means you are changing the default ckeditor files. To prevent this, we can customize the ckeditor using an external config file and keep the default ckeditor files intact. To apply the external config file,

CKEDITOR.replace( ' TextBox1',

    {

        customConfig : '/Custom/CodeDigest_config.js'

    });

 

CodeDigest_config.js can have your custom config settings.

 

Fetching the Text from CKEditor

Since, it is an ASP.Net textbox control it can be accessed by regular Text property. By default, ASP.Net will not allow HTML inputs to be posted to server in order to prevent the cross site scripting attach. You will get the following error,

A potentially dangerous Request.Form value was detected from the client (TextBox1="").

 

You need to set ValidateRequest="false" in the Page directive to prevent this error.

 

To fetch the text from javascript,

function GetText() {

            alert(CKEDITOR.instances.TextBox1.getData());          

        }

 

OR

function GetText() {

            alert(CKEDITOR.instances["TextBox1"].getData());          

        }

 

Refer here to know more above editor instance.

 

Working with Events

By default, CKEditor exposes number of events that can be hooked from the clientside. Refer the events section here.

 

Syntax

CKEDITOR.instances["TextBox1"].on("eventname", event method);

 

Example

CKEDITOR.instances["TextBox1"].on("instanceReady", InstanceReadyEvent);

     

        function InstanceReadyEvent() {

            this.document.on("keyup", function() {

                // var editor = CKEDITOR.instances.TextBox1;

                var editor = CKEDITOR.instances["TextBox1"];

                //Your code Goes here                

                var tex = editor.getData();

            });

 

        }

 

The above event attaches an onkeyup event on the editor.

 

Conclusion

Rich text editor is one of the main controls that is required if you build a CMS system or an application that requires rich commenting system. CKEditor is one of the good options when you compare with the other editor’s available online. The editor itself was fully re-written starting from 3.0 version that has drastically improved its performance and its feature set. Go ahead, try this cool editor you will definitely love this!!

Happy Coding!!

Similar Articles
  • You can contribute to CodeDigest.Com:
    Donate to CodeDigest.com
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments
    Doubled output
    Whatever content I enter in my instance of ckeditor comes back doubled when i hit 'Save'. I quadruple checked my code, but I don't get it. Can you help me?
    Image Upload
    Can you show how to configure the image upload part?